﻿using System;
using System.Collections.Generic;
using System.Net;

using Thrift.Transport;
using Thrift.Protocol;

using Apache.Cassandra;
using Client = Apache.Cassandra.Cassandra.Client;

using RexToy.DesignPattern;

namespace RexToy.Cassandra
{
    //Extend:Authentication/Login
    internal sealed class RawConnection : DisposableBase
    {
        private DnsEndPoint _ep;
        public DnsEndPoint EndPoint
        {
            get { return _ep; }
        }

        private TSocket _socket;
        private TFramedTransport _transport;
        private Client _c;
        public RawConnection(DnsEndPoint ep)
        {
            _ep = ep;
            _socket = new TSocket(ep.Host, ep.Port);
            _transport = new TFramedTransport(_socket);
            var protocol = new TBinaryProtocol(_transport);
            _c = new Client(protocol);
        }

        public Client Client
        {
            get
            {
                CheckDisposed();
                return _c;
            }
        }

        public void Open()
        {
            this.CheckDisposed();
            _socket.Open();
        }

        public void Close()
        {
            this.CheckDisposed();
            _socket.Close();
        }

        public bool IsOpen
        {
            get
            {
                this.CheckDisposed();
                return _socket.IsOpen;
            }
        }

        protected override void DisposeManagedObject()
        {
            _transport.Dispose();
            _socket.Dispose();
        }
    }
}
